package com.cmcc.aregister.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.aregister.R;

public class OrderAdapter extends BaseAdapter {
    private Context context;
    private int[] title;
    private int[] local_title = { R.string.your_order, R.string.hospital_1,
        R.string.subject_1, R.string.doctor_1, R.string.register_time_1,
        R.string.cost_1 };
    private int[] other_title = { R.string.your_order, R.string.demand_desc,
        R.string.empty, R.string.best_time };
    private String[] contentStr;
    private String guahao_amt;
    private List<List<String>> content = new ArrayList<List<String>>();
    public boolean clickAble;
    private boolean isLocal;
    private int clickPosition = -1;

    public OrderAdapter(Context context, String[] contentStr,
        String guahao_amt, boolean clickAble, boolean isLocal) {
        this.context = context;
        this.contentStr = contentStr;
        this.guahao_amt = guahao_amt;
        this.clickAble = clickAble;
        this.isLocal = isLocal;
        if (isLocal) {
            title = local_title;
        }
        else {
            title = other_title;
        }
    }

    public OrderAdapter(Context context, String[] contentStr,
        String guahao_amt, int clickPosition, boolean isLocal) {
        this.context = context;
        this.contentStr = contentStr;
        this.guahao_amt = guahao_amt;
        this.clickAble = true;
        this.isLocal = isLocal;
        this.clickPosition = clickPosition;
        if (isLocal) {
            title = local_title;
        }
        else {
            title = other_title;
        }
    }

    public void setData(String[] values, String guahao_amt, boolean clickAble) {
        contentStr = values;
        this.guahao_amt = guahao_amt;
        this.clickAble = clickAble;
    }

    static class ViewHolder {
        TextView title;
        TextView content;
        ImageView tag;
    }

    @Override
    public int getCount() {
        return title.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        if (clickAble) {
            if (position == 0 || position == getCount() - 1
                || position < clickPosition) {
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return false;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.a_text_text_image, null);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.tag = (ImageView) convertView.findViewById(R.id.tag);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(title[position]);
        if (position == 0) {
            holder.title.setTextColor(context.getResources().getColor(
                R.color.white));
            holder.content.setVisibility(View.INVISIBLE);
            holder.tag.setVisibility(View.INVISIBLE);
            convertView.setBackgroundResource(R.drawable.expandable_first);
        }
        else {
            if (position == getCount() - 1) {
                convertView.setBackgroundResource(R.drawable.expandable_last);
                holder.content.setVisibility(View.VISIBLE);
                holder.tag.setVisibility(View.GONE);
            }
            else {
                convertView.setBackgroundResource(R.drawable.expandable_center);
                holder.content.setVisibility(View.VISIBLE);
                holder.tag.setVisibility(View.VISIBLE);
            }
            if (!clickAble) {
                holder.tag.setVisibility(View.GONE);
            }
            holder.title.setTextColor(context.getResources().getColor(
                R.color.black));
            if (position == 5) {
                if (!"".equals(guahao_amt)) {
                    holder.content.setText(guahao_amt
                        + context.getText(R.string.yuan));
                }
            }
            else {
                holder.content.setText(contentStr[position - 1]);
            }
        }
        return convertView;
    }
}
